package org.linlinjava.litemall.wx.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.db.domain.IliquormallLibrary;
import org.linlinjava.litemall.db.service.ILiquormallLibraryService;
import org.linlinjava.litemall.wx.dto.LiquorLibraryCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class WxLiquorLibraryService {

    private final Log logger = LogFactory.getLog(WxLiquorLibraryService.class);

    @Autowired
    private ILiquormallLibraryService libraryService;

    public LiquorLibraryCondition queryConditionsList(String province, String type, String brand) {
        List<IliquormallLibrary> libraryList = libraryService.queryDistinctListByCondition(province, type, brand);
        return convert2ConditionList(province, type, brand, libraryList);
    }

    private LiquorLibraryCondition convert2ConditionList(String province, String type, String brand,
                                                         List<IliquormallLibrary> libraryList) {
        LiquorLibraryCondition condition = new LiquorLibraryCondition();

        List<String> provinceList = new ArrayList<>();
        List<String> typeList = new ArrayList<>();
        List<String> brandList = new ArrayList<>();

        if (!StringUtils.isEmpty(province)) {
            provinceList.add(province);
        } else {
            provinceList = libraryList
                    .stream()
                    .filter(library -> !Objects.isNull(library))
                    .map(IliquormallLibrary::getProvince)
                    .filter(provinceStr -> !StringUtils.isEmpty(provinceStr))
                    .distinct()
                    .collect(Collectors.toList());
        }
        if (!StringUtils.isEmpty(type)) {
            typeList.add(type);
        } else {
            typeList = libraryList
                    .stream()
                    .filter(library -> !Objects.isNull(library))
                    .map(IliquormallLibrary::getType)
                    .filter(typeStr -> !StringUtils.isEmpty(typeStr))
                    .distinct()
                    .collect(Collectors.toList());
        }
        if (!StringUtils.isEmpty(brand)) {
            brandList.add(brand);
        } else {
            brandList = libraryList
                    .stream()
                    .filter(library -> !Objects.isNull(library))
                    .map(IliquormallLibrary::getBrand)
                    .filter(brandStr -> !StringUtils.isEmpty(brandStr))
                    .distinct()
                    .collect(Collectors.toList());
        }

        condition.setProvinceList(provinceList);
        condition.setTypeList(typeList);
        condition.setBrandList(brandList);

        return condition;
    }


}
